/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Area;

/**
 *
 * @author JeM
 */
public class AreaDao {
    
    private Connection con;
    private PreparedStatement comando;

    public void insere(Area area){
           String sql = "INSERT INTO area(nome) " +
               "VALUES(?)";
       //Cria a conexao e gera o comando.
       conectar(sql);
       try {
           comando.setString(1, area.getNome());
         
           comando.execute();

       } catch (SQLException ex) {
            Logger.getLogger(AreaDao.class.getName()).log(Level.SEVERE, null, ex);
       } finally {
           //fecha o comando e a conexao
          fechar();
       }
    }

    public List<Area> getLista() {
        List<Area> areas = new ArrayList<Area>();
        conectar("select * from area");
        ResultSet rs;
        try {
            rs = comando.executeQuery();
            while (rs.next()) {
            	Area area = new Area();
            	area.setId(rs.getInt("id"));
            	areas.add(area);
            }
            rs.close();
        } catch (SQLException ex) {
            Logger.getLogger(AreaDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally{
            fechar();
        }
        return areas;
    }

    public Area getAreaById(int id)
    {
        String sql = "SELECT * FROM area WHERE id = ?";
        Area area = new Area();
        conectar(sql);
        ResultSet rs;
        try {

            comando.setInt(1, id);
            rs = comando.executeQuery();
            rs.next();

            area.setId(rs.getInt("id"));
            area.setNome(rs.getString("nome"));
            rs.close();
        } catch (SQLException ex) {
            Logger.getLogger(AreaDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally{
            fechar();
        }
        return area;
    }

    public Area getPessoaByNome(String nome)
    {
        String sql = "SELECT * FROM area WHERE nome = ?";
        Area area = new Area();
        conectar(sql);
        ResultSet rs;
        try {

            comando.setString(1, nome);
            rs = comando.executeQuery();
            rs.next();
            
            area.setId(rs.getInt("id"));
            area.setNome(rs.getString("nome"));
            rs.close();
        } catch (SQLException ex) {
            Logger.getLogger(AreaDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally{
            fechar();
        }
        return area;
    }

    public void atualiza(Area area) {
        String sql = "UPDATE area " +
                "SET nome=?, email=?, senha=?" +
                "WHERE id=?";
        conectar(sql);
        try {
            comando.setInt(1, area.getId());
            comando.setString(2, area.getNome());
 
            comando.execute();
        } catch (SQLException ex) {
            Logger.getLogger(AreaDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally{
            fechar();
        }
    }

    public void remove(Area area)
    {
        String sql = "DELETE FROM area WHERE id=?";
        conectar(sql);
        try {
            comando.setInt(1, area.getId());
            comando.execute();
        } catch (SQLException ex) {
            Logger.getLogger(AreaDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally{
            fechar();
        }

    }

    private void conectar(String sql) {
        try {
            con = ConnectionFactory.getConnection();
            comando = con.prepareStatement(sql);
        } catch (SQLException ex) {
            Logger.getLogger(AreaDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    private void fechar() {
        try {
            comando.close();
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(AreaDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }    
}
